﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Text;

namespace OpenCBMArchiver
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("OpenCBM Archiver");
            Console.WriteLine("http://opencbmarchiver.codeplex.com");


            Console.WriteLine("Execute with --help for help.");

            string drive = "8";
            string rootname = "disk";
            string seed = "0";
            string path = ".";
            string ext = "d64";
            string program = "d64copy.exe";
            string commandline = "{0} {1}";
            string readdir = "true";

            if (ConfigurationManager.AppSettings["Drive"] != null) drive = ConfigurationManager.AppSettings["Drive"];
            if (ConfigurationManager.AppSettings["Seed"] != null) seed = ConfigurationManager.AppSettings["Seed"];
            if (ConfigurationManager.AppSettings["Path"] != null) path = ConfigurationManager.AppSettings["Path"];
            if (ConfigurationManager.AppSettings["Name"] != null) rootname = ConfigurationManager.AppSettings["Name"];
            if (ConfigurationManager.AppSettings["Extension"] != null) ext = ConfigurationManager.AppSettings["Extension"];
            if (ConfigurationManager.AppSettings["Program"] != null) program = ConfigurationManager.AppSettings["Program"];
            if (ConfigurationManager.AppSettings["CommandLine"] != null) commandline = ConfigurationManager.AppSettings["CommandLine"];
            if (ConfigurationManager.AppSettings["ReadDirectory"] != null) readdir = ConfigurationManager.AppSettings["ReadDirectory"];

            for (int i = 0; i < args.Length; ++i)
            {
                switch (args[i].ToLower())
                {
                    case "--help":
                        ShowHelp();
                        return;

                    case "-d":
                        drive = args[++i];
                        break;

                    case "-n":
                        rootname = args[++i];
                        break;
                    case "-s":
                        seed = args[++i];
                        break;

                    case "-p":
                        path = args[++i];
                        break;

                    case "-e":
                        ext = args[++i];
                        break;

                    case "-o":
                        program = args[++i];
                        break;

                    case "-r":
                        readdir = args[++i];
                        break;
                }
            }

            var s = Convert.ToInt32(seed);
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration
                    (ConfigurationUserLevel.None);

            while (true)
            {
                Console.WriteLine("\n\nInsert disk, then hit any key, ESC to end.");
                Console.Out.Flush();

                var info = Console.ReadKey(true);
                if (info.Key == ConsoleKey.Escape) return;

                var filename = String.Format("{0}{1:0000}.{2}", rootname, s++, ext);

                var p = new ProcessStartInfo(string.Format("{0}\\cbmctrl", path),
                                             string.Format("dir {0}", drive));

                p.RedirectStandardOutput = false;
                p.RedirectStandardError = false;
                p.UseShellExecute = false;

                Process pr = null;
                if (Convert.ToBoolean(readdir))
                {
                    pr = Process.Start(p);

                    pr.WaitForExit();
                }


                config.AppSettings.Settings.Remove("Seed");
                config.AppSettings.Settings.Add("Seed", s.ToString());

                config.Save(ConfigurationSaveMode.Modified);

                var cl = string.Format(commandline, drive, filename);
                Console.WriteLine("Executing {1}\\{2} {0}", cl, path, program);
                p = new System.Diagnostics.ProcessStartInfo(string.Format("{0}\\{1}", path, program),
                                                                cl);

                p.RedirectStandardOutput = false;
                p.RedirectStandardError = false;
                p.UseShellExecute = false;

                pr = Process.Start(p);

                pr.WaitForExit();
            }
        }

        private static void ShowHelp()
        {
            Console.WriteLine("USAGE: OpenCBMArchiver -d {drive} -n {name} -s {seed} -e {extension} -p {OpenCBM Path} -o {Program}");
            Console.WriteLine();
            Console.WriteLine("\t-d Drive number (default 8)");
            Console.WriteLine("\t-n Base name (default \"disk\"");
            Console.WriteLine("\t-s Seed value (default 0)");
            Console.WriteLine("\t-e Extension (default \"d64\"");
            Console.WriteLine("\t-p OpenCBM Path (default \".\"");
            Console.WriteLine("\t-o Program to run (default \"d64copy.exe\")");
            Console.WriteLine("\t-r {true | false} Read directory before copy. (default \"true\")");
            Console.WriteLine("\nTo set the command line parameters, you must edit the \"CommandLine\" appSetting line in the configuration file.");
            Console.WriteLine();
        }
    }
}
